﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VTS.BLL;
using VTS.Common;
using VTS.DAL;

public partial class admin_Modules_Result_EditItem : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindDataForControls();

        if (!Page.IsPostBack)
        {
            string Id = Request["ItemId"];

            if (!string.IsNullOrEmpty(Id))
            {
                int itemId = 0;
                int.TryParse(Id, out itemId);

                if (itemId != 0)
                {
                    KQHocSinhBLL bllKQ = new KQHocSinhBLL();
                    KQHocSinh kqHocSinh = bllKQ.GetById(itemId);

                    txtLoaiKqHS.Text = kqHocSinh.LoaiKQ;
                    CKEditor1.Text = kqHocSinh.NoiDung;

                    ddlHocKy.Items.FindByValue(!kqHocSinh.HocKy.HasValue || !kqHocSinh.HocKy.Value ? "0" : "1").Selected = true;
                    ddlLop.Items.FindByValue(kqHocSinh.Lop.ToString()).Selected = true;
                    ddlNamHoc.Items.FindByValue(kqHocSinh.NamHoc.ToString()).Selected = true;


                }
            }
            else
            {
                Response.Redirect(Constants.DEFAULT_PAGE);
            }
        }
    }

    private void BindDataForControls()
    {
        NamHocBLL bllNamHoc = new NamHocBLL();
        ddlNamHoc.DataSource = bllNamHoc.GetAll();
        ddlNamHoc.DataValueField = "MaNH";
        ddlNamHoc.DataTextField = "NienKhoa";
        ddlNamHoc.DataBind();

        string currentYear = string.Concat(DateTime.Now.Year, " - ", DateTime.Now.Year + 1);
        ListItem item = ddlNamHoc.Items.FindByText(currentYear);
        if (item != null) item.Selected = true;

        LopBLL bllLop = new LopBLL();
        ddlLop.DataSource = bllLop.GetAll();
        ddlLop.DataTextField = "TenLop";
        ddlLop.DataValueField = "MaLop";
        ddlLop.DataBind();
        ddlLop.SelectedIndex = 0;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        KQHocSinhBLL bllKq = new KQHocSinhBLL();
        bllKq.Update(ddlNamHoc.SelectedValue, ddlHocKy.SelectedValue, ddlLop.SelectedValue,
                txtLoaiKqHS.Text, CKEditor1.Text, Request["ItemId"]);
        Response.Redirect(Constants.DEFAULT_PAGE);
    }
}